@echo off
chcp 65001 >nul
title Cross Network Server - One-Click Deployment

echo ============================================================
echo          Cross Network Data Server - One-Click Deploy
echo ============================================================
echo.

echo 🎯 This tool will:
echo    1. Deploy WebSocket server with data persistence to Aliyun ECS
echo    2. Configure 24/7 stable operation 
echo    3. Test all server functions
echo    4. Generate Android client configuration
echo    5. Provide complete deployment report
echo.

echo 📋 Prerequisites:
echo    - Aliyun ECS instance (2 cores, 4GB RAM, Ubuntu 20.04)
echo    - Security groups configured (ports 22, 80, 443, 8085)
echo    - SSH access to ECS instance
echo    - Python 3.6+ installed locally
echo.

echo 💰 Estimated Cost: ~80-120 RMB/month
echo.

set /p confirm="Ready to start deployment? (Y/N): "
if /i not "%confirm%"=="Y" (
    echo Deployment cancelled.
    pause
    exit /b 0
)

echo.
echo ==========================================
echo Step 1: Pre-deployment Check
echo ==========================================

REM Check if required files exist
set missing_files=0

if not exist "cross_network_server.py" (
    echo ❌ Missing: cross_network_server.py
    set missing_files=1
)

if not exist "deploy_cross_network_server.sh" (
    echo ❌ Missing: deploy_cross_network_server.sh
    set missing_files=1
)

if not exist "deploy_cross_network.py" (
    echo ❌ Missing: deploy_cross_network.py
    set missing_files=1
)

if %missing_files% equ 1 (
    echo.
    echo ❌ Some required files are missing. Please ensure all files are present.
    pause
    exit /b 1
)

echo ✅ All required files are present

REM Check Python installation
python --version >nul 2>&1
if %errorlevel% neq 0 (
    echo.
    echo ❌ Python is not installed or not in PATH
    echo Please install Python 3.6+ and try again
    pause
    exit /b 1
)

echo ✅ Python is available

echo.
echo ==========================================
echo Step 2: Automated Deployment
echo ==========================================

echo Running Python deployment script...
python deploy_cross_network.py

if %errorlevel% neq 0 (
    echo.
    echo ❌ Deployment failed. Please check the error messages above.
    echo.
    echo 🔧 Manual deployment options:
    echo    1. Run: .\deploy_cross_network_en.bat
    echo    2. Follow the step-by-step guide in ALIYUN_ECS_DEPLOYMENT_STEPS.md
    pause
    exit /b 1
)

echo.
echo ==========================================
echo Step 3: Connection Testing
echo ==========================================

REM Ask if user wants to test connection
set /p test_connection="Do you want to test the server connection? (Y/N): "
if /i "%test_connection%"=="Y" (
    set /p server_ip="Enter the ECS public IP address: "
    if not "!server_ip!"=="" (
        echo.
        echo Testing connection to ws://!server_ip!:8085...
        python test_cross_network_connection.py ws://!server_ip!:8085
        
        if !errorlevel! equ 0 (
            echo ✅ All connection tests passed!
        ) else (
            echo ⚠️ Some connection tests failed. Server might still be starting.
        )
    )
)

echo.
echo ==========================================
echo Step 4: Post-Deployment Instructions
echo ==========================================

echo.
echo 📱 Android Client Configuration:
echo    1. Update ServerConfig.kt with the new server address
echo    2. Or use the generated update_server_config.kt file
echo    3. Recompile APK: .\gradlew assembleRelease
echo    4. Install and test the app
echo.

echo 🔧 Server Management:
echo    - Check status: ssh root@[your-ip] "systemctl status cross-network-server.service"
echo    - View logs: ssh root@[your-ip] "journalctl -u cross-network-server.service -f"
echo    - Restart: ssh root@[your-ip] "systemctl restart cross-network-server.service"
echo.

echo 📊 Features Available:
echo    ✅ Data persistence (JSON files)
echo    ✅ Cross-network environment support
echo    ✅ Automatic monitoring and restart
echo    ✅ Daily backup at 2 AM
echo    ✅ 24/7 stable operation
echo.

echo 📋 Generated Files:
if exist "CROSS_NETWORK_DEPLOYMENT_REPORT.txt" (
    echo    ✅ CROSS_NETWORK_DEPLOYMENT_REPORT.txt - Deployment details
) else (
    echo    ❌ CROSS_NETWORK_DEPLOYMENT_REPORT.txt - Not generated
)

if exist "update_server_config.kt" (
    echo    ✅ update_server_config.kt - Android client configuration
) else (
    echo    ❌ update_server_config.kt - Not generated
)

echo.
echo ============================================================
echo              🎉 Deployment Process Complete!
echo ============================================================

echo.
echo Your cross-network data transfer server should now be running 24/7
echo on your Aliyun ECS instance. You can now use your Android app to
echo connect to the server from different network environments.
echo.

echo 🔗 Next Steps:
echo    1. Update your Android app configuration
echo    2. Test the app on different networks (WiFi, mobile data)
echo    3. Verify cross-network data synchronization
echo    4. Monitor server performance
echo.

echo 📞 Support:
echo    - Check deployment report for detailed logs
echo    - Review server logs if any issues occur
echo    - Test connection using the provided test script
echo.

pause